
import userApi from "@/api/user.js"
const TokenKey = "token";

// 查看是否获取过微信授权
export default {
    getToken() {
        return uni.getStorageSync(TokenKey);
    },
    setToken(token) {
        return uni.setStorageSync(TokenKey, token);
    },
    removeToken() {
        return uni.removeStorageSync(TokenKey);
    },

    getSetting () {
        return new Promise((resolve, reject) => {
            uni.getSetting({
                success(res) {
                    if (res.authSetting && res.authSetting['scope.userInfo']) {
                        resolve(true)
                        return
                    }
                    // 申请获取用户授权
                    uni.authorize({
                        scope: 'scope.userInfo',
                        success() {
                            // 授权成功
                            resolve(true)
                        },
                        fail(e) {
                            console.log(e)
                            resolve(false)
                        }
                    })
                }
            })
        })
    },
    // 检测用户登陆状态是否过期
    checkSession () {
        return new Promise((resolve, reject) => {
            uni.checkSession({
                success(d) {
                    console.log(333333)
                    console.log('未过期')
                    console.log('sessionCode', uni.getStorageSync('session_code'))
                    if (!uni.getStorageSync('sessionCode') || uni.getStorageSync(
                        'sessionCode').length < 0) {
                        // self.wxLogin()
                        resolve(false)
                        return false
                    }
                    resolve(true)
                    // getUserInfo()
                },
                fail(e) {
                    console.log('已经过期')
                    console.log(e)
                    resolve(true)
                }
            })
        })
    },
    // wx 登陆
    wxLogin () {
        return new Promise((resolve, reject) => {
            uni.login({
                provider: 'weixin',
                success: function(loginRes) {
                    // 本地缓存 code
                    uni.setStorageSync('sessionCode', loginRes.code)
                    resolve(true)
                },
                fail: function(err) {
                    resolve(false)
                }
            })
        })
    },
    // 云端获取用户信息
    getWxUserInfo () {
        return new Promise((resolve, reject) => {
            uni.getUserInfo({
                provider: 'weixin',
                success: function (infoRes) {
                    resolve(infoRes)
                },
                fail: function (err) {
                    reject(false)
                }
            });
        })
    },

    async getUserToken (infoRes) {
        return await userApi.login({
            code: uni.getStorageSync('sessionCode'),
            iv: infoRes.iv,
            encrypted_data: infoRes.encryptedData,
            type: 3,
            branch_uuid: "dd39f41b-61c2-472b-0a77-e7f273359c42"
        }).then(res => {
            if(res.code == 200){
                this.setToken(res.data.token)
            }
        })
    },

    // 登陆
    async authLogin () {
        const settingRes = await this.getSetting()
        if(!settingRes) return false
        // 检测session是否过期
        const checkSessionRes = await this.checkSession()
        if(!checkSessionRes) {
            // 进行微信登陆
            const wxLoginRes = await this.wxLogin()
            if(wxLoginRes) {
                const WxUserInfo = await this.getWxUserInfo()
                if(WxUserInfo) {
                    await this.getUserToken(WxUserInfo)
                }
            }
            return
        }
        const WxUserInfo = await this.getWxUserInfo()
        if(WxUserInfo) {
            await this.getUserToken(WxUserInfo)
        }
    }

}
